/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "outlineview.h"

#include "basketiconprovider.h"

#include "removerecursiveworker.h"

#include <QSettings>
#include "settings.h"

#include <QTreeView>
#include <QHeaderView>

#include <QFileSystemModel>
#include <QStandardItemModel>
#include <QFileInfo>
#include <QDir>

#include <QMenu>
#include <QAction>
#include <QInputDialog>
#include <QMessageBox>

#include <QtDebug>

OutlineView::OutlineView(QWidget* parent /*= 0*/)
		:TreeView(parent)
{

	setupRootDir();
	setupOutline();
	setupContextMenu();

	connect(this, SIGNAL(clicked(QModelIndex)), this, SLOT(basketSelected(QModelIndex)));

	qDebug() << "Root-path for data:" << getDataRoot().absolutePath();

}


QDir OutlineView::getDataRoot()
{
	QSettings	settings;
	return QDir(settings.value(SETTING_DATA_ROOT).toString());
}


void OutlineView::setupRootDir()
{
	QFileInfo root(getDataRoot().absolutePath());

	if (!root.exists())
	{
		QDir rootDir;
		rootDir.mkdir(getDataRoot().absolutePath());
	}


}

void OutlineView::basketSelected(const QModelIndex & index)
{
	QFileInfo newPath = mFileModel->fileInfo(index);

	emit LoadBasket(newPath);

}

void OutlineView::setupContextMenu()
{

	QAction* pAction = 0;

	mBasketContextMenu = new QMenu(this);
	mRootMenu		   = new QMenu(this);

	pAction = new QAction(tr("Create Basket", "outline"), this);
	mBasketContextMenu->addAction(pAction);
	connect (pAction, SIGNAL(triggered()), this, SLOT(createBasket()));

	pAction = new QAction(tr("Rename Basket", "outline"), this);
	mBasketContextMenu->addAction(pAction);
	connect(pAction, SIGNAL(triggered()), this, SLOT(renameBasket()));

	pAction = new QAction(tr("Delete Basket", "outline"), this);
	mBasketContextMenu->addAction(pAction);
	connect(pAction, SIGNAL(triggered()), this, SLOT(deleteBasket()));

	pAction = new QAction(tr("Create Root Basket", "outline"), this);
	mBasketContextMenu->addAction(pAction);
	mRootMenu->addAction(pAction);


	connect(pAction, SIGNAL(triggered()), this, SLOT(createRootBasket()));

}

void OutlineView::setupOutline()
{
	mFileModel = new QFileSystemModel(this);

	mFileModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
	mFileModel->setIconProvider(new BasketIconProvider);

	setModel(mFileModel);
	setRootIndex(mFileModel->setRootPath(getDataRoot().path()));

	// We don't whant all the "crapp" about file-size and so. Only the dir-tree
	// as this is our basket outline.
	for (int i = 1; i <= header()->count(); i++)
	{
		hideColumn(i);
	}

	connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(outlineContextMenu(QPoint)));

}


void OutlineView::outlineContextMenu(QPoint click)
{
	mContextMenuItem = indexAt(click);

	if (mContextMenuItem.isValid())
	{
		qDebug() << "Context menu for a basket is requested: " << mFileModel->fileInfo(mContextMenuItem).filePath();
		mBasketContextMenu->move(mapToGlobal(click));
		mBasketContextMenu->show();
	}
	else
	{
		qDebug() << "Context menu for no.basket requested";
		mRootMenu->move(mapToGlobal(click));
		mRootMenu->show();
	}
}

void OutlineView::outlineContextMenuClose()
{
	mContextMenuItem = QModelIndex();
}




void OutlineView::createBasket()
{
	bool bOk = false;
	QString basketName = QInputDialog::getText(this,
											   tr("New Basket", "outline"),
											   tr("Enter name for new basket", "outline"),
											   QLineEdit::Normal,
											   tr("NewBasket", "outline"),
											   &bOk);

	QDir dir = mFileModel->fileInfo(mContextMenuItem).filePath();

	qDebug() << "Create new basket in:" << dir.filePath(basketName);

	dir.mkdir(basketName);

}

void OutlineView::renameBasket()
{
	bool bOk = false;

	QFileInfo basket = mFileModel->fileInfo(mContextMenuItem);

	QString basketName = QInputDialog::getText(this,
											   tr("New Basket", "outline"),
											   tr("Enter name for new basket", "outline"),
											   QLineEdit::Normal,
											   basket.fileName(),
											   &bOk);


	if (bOk && (basketName != basket.fileName()))
	{
		emit aboutToChangeBasket(basket);

		QFileInfo oldBasket = basket;
		basket.setFile(oldBasket.dir(), basketName);

		qDebug() << "Oldname: " << oldBasket.filePath() << "  new name: " << basket.filePath();

		QDir directory(oldBasket.filePath());

		directory.rename(oldBasket.filePath(), basket.filePath());

		emit basketChanged(oldBasket, basket);


	}



}

void OutlineView::deleteBasket()
{

	QFileInfo basket = mFileModel->fileInfo(mContextMenuItem);

	QMessageBox confirmDelete;
	confirmDelete.setText(tr("Delete Basket", "outline"));
	confirmDelete.setInformativeText(tr("Delete basket \"%1\" and all descending baskets?", "outline").arg(basket.fileName()));
	confirmDelete.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	confirmDelete.setDefaultButton(QMessageBox::Cancel);
	int response = confirmDelete.exec();

	if (QMessageBox::Ok == response)
	{
		emit aboutToChangeBasket(basket);

		qDebug() << "Removing basket: " << basket.filePath();

		RemoveRecursiveWorker removeWorker;

		removeWorker.removeRecursive(basket);


		emit basketChanged(basket, QFileInfo());

	}
}



void OutlineView::createRootBasket()
{
	bool bOk = false;
	QString basketName = QInputDialog::getText(this,
											   tr("New Root Basket"),
											   tr("Enter name for new root basket"),
											   QLineEdit::Normal, "NewBasket",
											   &bOk);

	if (bOk)
	{
		qDebug() << "Create new basket in:" << getDataRoot().filePath(basketName);
		getDataRoot().mkdir(basketName);
	}

}
